<template>
  <a-modal
    title="问卷题目管理"
    :width="1500"
    :visible="visible"
    :confirmLoading="confirmLoading"
    :footer="null"
    @cancel="handleCancel"
  >
    <div>
      <a-card :bordered="false" :bodyStyle="tstyle">
        <div class="table-page-search-wrapper" v-if="hasPerm('questionnaireSubject:page')">
          <a-form layout="inline">
            <a-row :gutter="48">
              <a-col :md="8" :sm="24">
                <a-form-item label="题目">
                  <a-input v-model="queryParam.title" allow-clear placeholder="请输入题目"/>
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="题目类型">
                  <a-select style="width: 100%" v-model="queryParam.type" placeholder="请选择题目类型">
                    <a-select-option v-for="(item,index) in questionnaireSubjectTypeData" :key="index" :value="item.code">{{ item.name }}</a-select-option>
                  </a-select>
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="是否甄别题目">
                  <a-select style="width: 100%" v-model="queryParam.isScreening" placeholder="请选择是否甄别题目">
                    <a-select-option v-for="(item,index) in whetherData" :key="index" :value="item.code">{{ item.name }}</a-select-option>
                  </a-select>
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24" >
                <span class="table-page-search-submitButtons">
                  <a-button type="primary" @click="$refs.table.refresh(true)" >查询</a-button>
                  <a-button style="margin-left: 8px" @click="() => queryParam = {}">重置</a-button>
                </span>
              </a-col>
            </a-row>
          </a-form>
        </div>
      </a-card>
      <a-card :bordered="false">
        <s-table
          ref="table"
          :columns="columns"
          :data="loadData"
          :alert="options.alert"
          :rowKey="(record) => record.id"
          :rowSelection="options.rowSelection"
        >
          <template class="table-operator" slot="operator" v-if="hasPerm('questionnaireSubject:add')" >
            <a-button type="primary" v-if="hasPerm('questionnaireSubject:add')" icon="plus" @click="$refs.addForm.add(queryParam.questionnaireId)">新增问卷题目表</a-button>
            <a-button type="danger" :disabled="selectedRowKeys.length < 1" v-if="hasPerm('questionnaireSubject:delete')" @click="batchDelete"><a-icon type="delete"/>批量删除</a-button>
            <x-down
              v-if="hasPerm('questionnaireSubject:export')"
              ref="batchExport"
              @batchExport="batchExport"
            />
          </template>
          <span slot="typeScopedSlots" slot-scope="text">
            {{ 'questionnaire_subject_type' | dictType(text) }}
          </span>
          <span slot="isScreeningScopedSlots" slot-scope="text">
            {{ 'whether' | dictType(text) }}
          </span>
          <span slot="action" slot-scope="text, record">
            <a v-if="hasPerm('questionnaireSubject:edit')" @click="$refs.editForm.edit(record)">编辑</a>
            <a-divider type="vertical" v-if="hasPerm('questionnaireSubject:edit') & hasPerm('questionnaireSubject:delete')"/>
            <a-popconfirm v-if="hasPerm('questionnaireSubject:delete')" placement="topRight" title="确认删除？" @confirm="() => singleDelete(record)">
              <a>删除</a>
            </a-popconfirm>
          </span>

          <span slot="action" slot-scope="text, record">
          <a v-if="hasPerm('questionnaireSubject:edit')" @click="$refs.editForm.edit(record)">编辑</a>
          <a-divider type="vertical" v-if="hasPerm('questionnaireSubject:edit') || hasPerm('questionnaireSubject:delete') || hasPerm('questionnaireSubjectAnswer:page')"/>
          <a-dropdown v-if="hasPerm('questionnaire:delete') || hasPerm('questionnaireSubjectAnswer:page')">
            <a class="ant-dropdown-link">
              更多 <a-icon type="down" />
            </a>
            <a-menu slot="overlay">
              <a-menu-item v-if="hasPerm('questionnaireSubjectAnswer:page')">
                <a @click="$refs.questionnaireSubjectAnswer.init(record.id)">答案管理</a>
              </a-menu-item>
              <a-menu-item v-if="hasPerm('questionnaireSubject:delete')">
                <a-popconfirm placement="topRight" title="确认删除？" @confirm="() => singleDelete(record)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>
        </s-table>
        <add-form ref="addForm" @ok="handleOk" />
        <edit-form ref="editForm" @ok="handleOk" />
        <questionnaire-subject-answer ref='questionnaireSubjectAnswer' />
      </a-card>
    </div>
  </a-modal>
</template>
<script>
  import { STable, XDown } from '@/components'
  import { questionnaireSubjectPage, questionnaireSubjectDelete, questionnaireSubjectExport } from '@/api/modular/main/questionnairesubject/questionnaireSubjectManage'
  import addForm from './addForm.vue'
  import editForm from './editForm.vue'
  import questionnaireSubjectAnswer from '../questionnairesubjectanswer/index'
  export default {
    components: {
      STable,
      addForm,
      editForm,
      XDown,
      questionnaireSubjectAnswer
    },
    data () {
      return {
        // 高级搜索 展开/关闭
        advanced: false,
        // 查询参数
        queryParam: {},
        // 表头
        columns: [
          {
            title: '题目',
            align: 'center',
            dataIndex: 'title'
          },
          {
            title: '题目类型',
            align: 'center',
            dataIndex: 'type',
            scopedSlots: { customRender: 'typeScopedSlots' }
          },
          {
            title: '是否甄别题目',
            align: 'center',
            dataIndex: 'isScreening',
            scopedSlots: { customRender: 'isScreeningScopedSlots' }
          }
        ],
        tstyle: { 'padding-bottom': '0px', 'margin-bottom': '10px' },
        // 加载数据方法 必须为 Promise 对象
        loadData: parameter => {
          return questionnaireSubjectPage(Object.assign(parameter, this.queryParam)).then((res) => {
            return res.data
          })
        },
        questionnaireSubjectTypeData: [],
        whetherData: [],
        selectedRowKeys: [],
        selectedRows: [],
        options: {
          alert: { show: true, clear: () => { this.selectedRowKeys = [] } },
          rowSelection: {
            selectedRowKeys: this.selectedRowKeys,
            onChange: this.onSelectChange
          }
        },
        visible: false,
        confirmLoading: false
      }
    },
    created () {
      if (this.hasPerm('questionnaireSubject:edit') || this.hasPerm('questionnaireSubject:delete')) {
        this.columns.push({
          title: '操作',
          width: '150px',
          dataIndex: 'action',
          scopedSlots: { customRender: 'action' }
        })
      }
      this.whetherData = this.$options.filters['dictData']('whether')
      this.questionnaireSubjectTypeData = this.$options.filters['dictData']('questionnaire_subject_type')
    },
    methods: {
      /**
       * 单个删除
       */
      singleDelete (record) {
        const param = [{ 'id': record.id }]
        this.questionnaireSubjectDelete(param)
      },
      /**
       * 批量删除
       */
      batchDelete () {
        const paramIds = this.selectedRowKeys.map((d) => {
            return { 'id': d }
        })
        this.questionnaireSubjectDelete(paramIds)
      },
      questionnaireSubjectDelete (record) {
        questionnaireSubjectDelete(record).then((res) => {
          if (res.success) {
            this.$message.success('删除成功')
            this.$refs.table.clearRefreshSelected()
          } else {
            this.$message.error('删除失败') // + res.message
          }
        })
      },
      toggleAdvanced () {
        this.advanced = !this.advanced
      },
      /**
       * 批量导出
       */
      batchExport () {
        const paramIds = this.selectedRowKeys.map((d) => {
            return { 'id': d }
        })
        questionnaireSubjectExport(paramIds).then((res) => {
            this.$refs.batchExport.downloadfile(res)
        })
      },
      handleOk () {
        this.$refs.table.refresh()
      },
      onSelectChange (selectedRowKeys, selectedRows) {
        this.selectedRowKeys = selectedRowKeys
        this.selectedRows = selectedRows
      },
      handleCancel () {
        this.visible = false
      },
      init (id) {
        this.visible = true
        this.queryParam.questionnaireId = id
        this.handleOk()
      },
    }
  }
</script>
<style lang="less">
  .table-operator {
    margin-bottom: 18px;
  }
  button {
    margin-right: 8px;
  }
</style>
